package com.yiren.dbaa.module.device.model;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.bluetooth.BluetoothDevice;

import com.yiren.backstage.communication.api.impl.BluetoothCommunicationApiImpl;
import com.yiren.backstage.communication.interfaces.DataCallback;
import com.yiren.backstage.communication.utils.BluetoothUtil;
import com.yiren.backstage.currency.constant.SignalGConstant;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.device.contract.BlueToothContract;

import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * Created by WANGRONGXIANG
 * on 2022/7/1
 * Describe
 */
public class BlueToothModel implements BlueToothContract.Model {

    private Activity activity;

    public BlueToothModel(Activity activity) {
        this.activity = activity;
    }

    @Override
    @SuppressLint("MissingPermission")
    public void getDeviceList(CommonCallBack callBack) {
        List<String> deviceList = new LinkedList<String>();
        Set<BluetoothDevice> btDevices = BluetoothUtil.getInstance().getBondedDevices();
        for (BluetoothDevice btDevice : btDevices) {
            deviceList.add(btDevice.getName() + "#" + btDevice.getAddress());
        }
        callBack.success(deviceList);
    }

    @Override
    public void connectBluetooth(String btDeviceName, CommonCallBack callBack) {
        try {
            String[] info = btDeviceName.split("#");
//                device = BluetoothUtil.getInstance().theBluetoothDevice(info[0], info[1]);
            BluetoothCommunicationApiImpl.getInstance(activity).setParam(info[0], info[1]);
            BluetoothCommunicationApiImpl.getInstance(activity).start();
            BluetoothCommunicationApiImpl.getInstance(activity).setDataCallback(new DataCallback() {
                @Override
                public void onStateChange(int state, String message) {
                    super.onStateChange(state, message);
                    if (message.contains("解析中")) {
                        callBack.complete();
                        SignalGConstant.isOpen = true;
                        activity.runOnUiThread(() -> callBack.success("蓝牙打开成功!"));
                    }
                }
            });
        } catch (Exception e) {
            callBack.fail("蓝牙打开失败");
            e.printStackTrace();
        }
    }

    @Override
    public void disconnectBluetooth(CommonCallBack callBack) {
        BluetoothCommunicationApiImpl.getInstance(activity).stop();
        BluetoothCommunicationApiImpl.getInstance(activity).setDataCallback(new DataCallback() {
            @Override
            public void onStateChange(int state, String message) {
                super.onStateChange(state, message);
                if (message.contains("已断开")) {
                    SignalGConstant.allData = "";
                    SignalGConstant.isOpen = false;
                    activity.runOnUiThread(() -> callBack.success("蓝牙已关闭!"));
                }
            }
        });
    }

    @Override
    public void sendData(byte[] data) {
        BluetoothCommunicationApiImpl.getInstance(activity).writeData(data, "", null);
    }


}
